/*
 * $Id: TreeWrapperDemoFrame.java,v 1.2 2006-06-06 10:18:48 antonio Exp $
 * Read the "license.txt" file for licensing information.
 * (C) Antonio Vieiro. All rights reserved.
 */
package net.antonioshome.swing.treewrapper.example;

import java.awt.Cursor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import javax.swing.Icon;
import javax.swing.UIDefaults;
import net.antonioshome.swing.treewrapper.CustomPopupHandler;
import net.antonioshome.swing.treewrapper.TreeWrapper;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;

/**
 * A simple frame that contains two trees.
 * @author Antonio Vieiro (antonio@antonioshome.net), $Author: antonio $
 * @version $Revision: 1.2 $
 */
public class TreeWrapperDemoFrame extends javax.swing.JFrame
{
  private TreeWrapper wrapper1;
  private TreeWrapper wrapper2;
  
  /**
   * Creates new form TreeWrapperDemoFrame
   */
  public TreeWrapperDemoFrame()
  {
    initComponents();
    // Create a wrapper for tree 1
    wrapper1 = new TreeWrapper( tree1 );
    // Create a wrapper for tree 2
    wrapper2 = new TreeWrapper( tree2 );
    // Add apples and oranges to first tree
    tree1.setModel( TreeModelBuilder.createModel() );
    // And to second tree too
    tree2.setModel( TreeModelBuilder.createModel() );
    // Add a custom cell renderer to tree 1
    tree1.setCellRenderer( new MyCustomCellRendererer() );
    // Add a custom popup handler to tree 1
    wrapper1.setCustomPopupHandler( new MyCustomPopupHandler() );
    
    setSize( 500, 350 );
    setLocationRelativeTo( null );
  }
  
  private JTree treeToShuffleOn;
  private TreeNode nodeToShuffle;
  
  private JTree treeToDeleteFrom;
  private TreeNode nodeToDelete;
  
  class MyCustomPopupHandler
    implements CustomPopupHandler
  {
    public JPopupMenu getMenuAt(JTree aJTree, TreeNode aTreeNode)
    {
      treeToDeleteFrom = null;
      nodeToDelete = null;
      treeToShuffleOn = null;
      nodeToShuffle = null;
      if ( ! aTreeNode.isLeaf() )
      {
        treeToShuffleOn = aJTree;
        nodeToShuffle = aTreeNode;
        mnuShuffleItems.setEnabled( aTreeNode.getChildCount() > 0 );
        return folderPopupMenu;
      }
      else
      {
        treeToDeleteFrom = aJTree;
        nodeToDelete = aTreeNode;
        return leafPopupMenu;
      }
    }
  }
  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  private void initComponents()
  {
    java.awt.GridBagConstraints gridBagConstraints;

    folderPopupMenu = new javax.swing.JPopupMenu();
    mnuShuffleItems = new javax.swing.JMenuItem();
    leafPopupMenu = new javax.swing.JPopupMenu();
    mnuDeleteNode = new javax.swing.JMenuItem();
    jScrollPane1 = new javax.swing.JScrollPane();
    tree1 = new javax.swing.JTree();
    jScrollPane2 = new javax.swing.JScrollPane();
    tree2 = new javax.swing.JTree();
    jTextField1 = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    optDisableTree = new javax.swing.JCheckBox();

    mnuShuffleItems.setText("Shuffle items...");
    mnuShuffleItems.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        mnuShuffleItemsActionPerformed(evt);
      }
    });

    folderPopupMenu.add(mnuShuffleItems);

    mnuDeleteNode.setText("Delete this node");
    mnuDeleteNode.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        mnuDeleteNodeActionPerformed(evt);
      }
    });

    leafPopupMenu.add(mnuDeleteNode);

    getContentPane().setLayout(new java.awt.GridBagLayout());

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("TreeWrapper demo");
    tree1.setAutoscrolls(true);
    jScrollPane1.setViewportView(tree1);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
    getContentPane().add(jScrollPane1, gridBagConstraints);

    tree2.setAutoscrolls(true);
    jScrollPane2.setViewportView(tree2);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
    getContentPane().add(jScrollPane2, gridBagConstraints);

    jTextField1.setText("Write some text here and drag it into the trees (or drag nodes between trees)");
    jTextField1.setDragEnabled(true);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridwidth = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.weightx = 10.0;
    gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
    getContentPane().add(jTextField1, gridBagConstraints);

    jLabel1.setText("Tree with popup menus");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
    getContentPane().add(jLabel1, gridBagConstraints);

    jLabel2.setText("Tree without popup menus");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
    getContentPane().add(jLabel2, gridBagConstraints);

    optDisableTree.setText("Disable tree");
    optDisableTree.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
    optDisableTree.setMargin(new java.awt.Insets(0, 0, 0, 0));
    optDisableTree.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        optDisableTreeActionPerformed(evt);
      }
    });

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.gridwidth = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
    getContentPane().add(optDisableTree, gridBagConstraints);

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void optDisableTreeActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_optDisableTreeActionPerformed
  {//GEN-HEADEREND:event_optDisableTreeActionPerformed

    tree1.setEnabled( ! optDisableTree.isSelected() );
    
  }//GEN-LAST:event_optDisableTreeActionPerformed

  private void mnuShuffleItemsActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_mnuShuffleItemsActionPerformed
  {//GEN-HEADEREND:event_mnuShuffleItemsActionPerformed

    if ( treeToShuffleOn != null && nodeToShuffle != null )
    {
      DefaultTreeModel model = (DefaultTreeModel) treeToShuffleOn.getModel();
      Enumeration children = nodeToShuffle.children();
      ArrayList nodesToShuffle = new ArrayList();
      while( children.hasMoreElements() )
      {
        MutableTreeNode child = (MutableTreeNode) children.nextElement();
        nodesToShuffle.add( child );
        model.removeNodeFromParent( child );
      }
      Collections.shuffle( nodesToShuffle );
      for( int i=0; i<nodesToShuffle.size(); i++ )
      {
        model.insertNodeInto( (MutableTreeNode) nodesToShuffle.get(i),
          (MutableTreeNode) nodeToShuffle, 0 );
      }
      treeToShuffleOn = null;
      nodeToShuffle = null;
    }
    

  }//GEN-LAST:event_mnuShuffleItemsActionPerformed
  
  private void mnuDeleteNodeActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_mnuDeleteNodeActionPerformed
  {//GEN-HEADEREND:event_mnuDeleteNodeActionPerformed
    
    if ( treeToDeleteFrom != null && nodeToDelete != null )
    {
      DefaultTreeModel model = (DefaultTreeModel) treeToDeleteFrom.getModel();
      model.removeNodeFromParent( (MutableTreeNode) nodeToDelete );
      treeToDeleteFrom = null;
      nodeToDelete = null;
    }
    
  }//GEN-LAST:event_mnuDeleteNodeActionPerformed
  
  /**
   * @param args the command line arguments
   */
  public static void main(String args[])
  {

    java.awt.EventQueue.invokeLater(new Runnable()
    {
      public void run()
      {
        new TreeWrapperDemoFrame().setVisible(true);
      }
    });
  }
  
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JPopupMenu folderPopupMenu;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JScrollPane jScrollPane2;
  private javax.swing.JTextField jTextField1;
  private javax.swing.JPopupMenu leafPopupMenu;
  private javax.swing.JMenuItem mnuDeleteNode;
  private javax.swing.JMenuItem mnuShuffleItems;
  private javax.swing.JCheckBox optDisableTree;
  private javax.swing.JTree tree1;
  private javax.swing.JTree tree2;
  // End of variables declaration//GEN-END:variables
  
}
